<?php
/*************************************************************************************
 *
 *	FileName: header_php.php
 *	Desc    : search the product for review
 *	Author  : Seal
 *	Date    : 2010-1-30
 *
 *************************************************************************************/
	require_once ( LIB_DIR.'classes/Pager.php' );
	
	require_once ( LIB_DIR.'search/productsearch.php' );
	
	
	if( !isset($_REQUEST['s_keyword']) || !isset($_REQUEST['sid']))
	{
		echo "无效的关键字或没有店铺ID！";
		edp_exit();
	}

	
	$keyword = $_REQUEST['s_keyword'];
	$sid = intval( $_REQUEST['sid'] );
	
	$page = 1;
	
    if( isset( $_GET['p'] ) ){
        $page = intval( $_GET['p'] );
	}

	$productSearch = new ProductSearch();
	$searchType = NORMAL_QUERY;

	//clean keyword
	$keyword = $productSearch->CleanKeyword( $keyword );
	
	// move page var keyword forward
	$pageVars['keyword'] = $keyword;

	//determine the search type
	$filterArray = array();
	

	$result = array();

	$filterArray['shop_id'] = array( $sid );
	
	$result = $productSearch->MultiFilterQuery( $keyword, $filterArray, $page );
	
	
	$pageVars['result'] = $result;
	$productDAO = GetProductDAO();
	$search_result = array();

	if( $result['total'] > 0 && !empty($result['matchIds']) ) 
	{
		$search_result = $productDAO->GetProductSimpleInfo( $result['matchIds'] );
		if( $search_result == false )
			$search_result = array();
	}

	if( $page == 1 ){
		$newAddedProductIds = $productDAO->GetRecentAddedProductIds( $sid, $keyword );
		if( $newAddedProductIds != false ) {
			$ids = array();
			foreach( $newAddedProductIds as $key => $value )
				if( !in_array($value['id'], $result['matchIds']) )
					$ids[] = $value['id'];
			if( count($ids) > 0 ) {		
				$newAddedProducts = $productDAO->GetProductSimpleInfo( $ids );
				if( $newAddedProducts == false )
					$newAddedProducts = array();
				$search_result = array_merge( $newAddedProducts, $search_result );
				$pageVars['result']['total'] += count($newAddedProducts);
			}
		}
	}
	
	if( count($search_result) == 0 )
		unset( $search_result );
 	
	$pageObj = new Pager( edp_href_link("ajax_product_in_shop", "tiny=1&s_keyword=" . urlencode($keyword) . "&sid=$sid"),
			$pageVars['result']['total'], SHOP_INFO_PER_PAGE, $page, UP_RESULT_PAGE );	
	
	
?>
